/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kelin.calendarlistview;

import com.kelin.calendarlistview.library.BaseCalendarItemAdapter;
import com.kelin.calendarlistview.library.BaseCalendarItemModel;

import ohos.agp.components.*;
import ohos.agp.components.element.VectorElement;
import ohos.agp.utils.Color;

import ohos.app.Context;

import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

/**
 * Created by kelin on 16-7-19.
 * CalendarItemAdapter
 *
 * @since 2021.07.28
 */
public class CalendarItemAdapter extends BaseCalendarItemAdapter<CustomCalendarItemModel> {

    /**
     * CalendarItemAdapter
     *
     * @param context 上下文
     */
    public CalendarItemAdapter(Context context) {
        super(context);
    }

    @Override
    public Component getView(String date, CustomCalendarItemModel model, Component convertView, ComponentContainer parent) {
        ComponentContainer view = (ComponentContainer) LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_griditem_custom_calendar_item, null, false);

        Text dayNum = (Text) view.findComponentById(ResourceTable.Id_tv_day_num);
        dayNum.setText(model.getDayNumber());

        view.setBackground(new VectorElement(mContext, ResourceTable.Graphic_bg_shape_calendar_item_normal));

        Color redColor = Color.RED;
        Color graybColor = Color.GRAY;
        try {
            redColor = new Color(mContext.getResourceManager().getElement(ResourceTable.Color_red_ff725f).getColor());
            graybColor = new Color(mContext.getResourceManager().getElement(ResourceTable.Color_gray_bbbbbb).getColor());
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }

        if (model.isToday()) {
            dayNum.setTextColor(redColor);
            dayNum.setText(ResourceTable.String_today);
        }

        if (model.isHoliday()) {
            dayNum.setTextColor(redColor);
        }


        if (model.getStatus() == BaseCalendarItemModel.Status.DISABLE) {
            dayNum.setTextColor(Color.DKGRAY);
        }

        if (!model.isCurrentMonth()) {
            dayNum.setTextColor(graybColor);
            view.setClickable(true);
        }

        Text dayNewsCount = (Text) view.findComponentById(ResourceTable.Id_tv_day_new_count);
        if (model.getNewsCount() > 0) {
            try {
                dayNewsCount.setText(String.format(mContext.getResourceManager().getElement(ResourceTable.String_calendar_item_new_count).getString(), model.getNewsCount()));
            } catch (IOException | NotExistException | WrongTypeException e) {
                e.printStackTrace();
            }
            dayNewsCount.setVisibility(Component.VISIBLE);
        } else {
            dayNewsCount.setVisibility(Component.HIDE);
        }

        Image isFavImageView = (Image) view.findComponentById(ResourceTable.Id_image_is_fav);
        if (model.isFav()) {
            isFavImageView.setVisibility(Component.VISIBLE);
        } else {
            isFavImageView.setVisibility(Component.HIDE);
        }

        return view;
    }
}
